$(function(){
	$('.list-content-div table thead tr th:last').attr("width","12%");
});

var path = $('body').attr('ctxPath');

//点击新增跳转
$('#newProject').on('click',function(){
	//查询当前单据类型是否有编码规则
	var url = path+'/project/checkCodingRule?_t='+new Date().getTime();
	$.post(url,function(data){
		if(!data.flag){
			layer.alert(data.msg);//提示
			return;
		}else{
			//跳转到 新增计划页面
			location.href=path+"/project/newProject";
		}
	});
});


//点击删除
$('.list-content-div table>tbody').delegate('tr>td>.delete', 'click', function() {
	var id = $(this).attr("data-id");
	//点击删除,先判断项目是否有计划,
/*	$.post(path+'/project/searchPlan/'+id+'?_t='+new Date().getTime(),function(data){
		if(!data.flag){
			layer.alert(data.msg);
			return;
		}else{*/
			var url = path+"/project/delete/"+id+"?_t="+new Date().getTime();
			//询问框
			layer.confirm('确定删除该记录吗？', {
			  btn: ['确认','取消'] //按钮
			}, function(){
				//根据id 删除对应数据
				$.post(url,function(data){
					if(data.flag){
						layer.msg(data.msg);
						listui.refresh();
					}else{
						layer.msg(data.msg);
					}
				});
			});
/*		}
	});*/
});


//启动,暂停
$('.list-content-div table>tbody').delegate('tr>td>.started', 'click', function() {
	var id = $(this).attr('data-id');
	var container = $(this).closest('.listui');
	var val = container.context.innerText;
	var started =  val == "启动" ? 0 : 1;
	//发请求更改状态.
	$.post(path+'/project/start?_t+'+new Date().getTime(),{"id":id,"started":started},function(data){
		if(data.flag){
			layer.msg(data.msg);
			listui.refresh();
		}else{
			layer.msg(data.msg);
		}
	})
});


//数据表格操作按钮
function handleOpRow(tr, obj, container) {
	if(obj.started==1){
		
		tr.append($('<td/>').addClass('op').append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('started').text('启动')).append($('<span/>').text(' | ')).append($('<a/>', {
			"href" : $('body').attr('ctxPath')+'/project/editProject/'+obj.id+'?_t='+new Date().getTime(),
			"data-id" : obj.id
		}).addClass('edits').text('编辑')).append($('<span/>').text(' | ')).append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('delete').text('删除')));
	}else if(obj.started==0){
		tr.append($('<td/>').addClass('op').append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('started').text('暂停')).append($('<span/>').text(' | ')).append($('<a/>', {
			"href" : $('body').attr('ctxPath')+'/project/editProject/'+obj.id+'?_t='+new Date().getTime(),
			"data-id" : obj.id
		}).addClass('edits').text('编辑')).append($('<span/>').text(' | ')).append($('<a/>', {
			"href" : "javascript:;",
			"data-id" : obj.id
		}).addClass('delete').text('删除')));
	}
}

//自定义表格信息
function handleCellValue(tfNotUnderline, cellVal, obj) {// (列名，单元格值，行对象)
	if (tfNotUnderline == "started") {
		if (cellVal == 0) {
			return '已启动';
		} else {
			return '未启动';
		}
	} else {
		return cellVal;
	}
}